package com.sanchunhui.psc.ui.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.RecyclerView
import com.eightbitlab.rxbus.Bus
import com.sanchunhui.psc.R
import com.sanchunhui.psc.common.BaseConstant
import com.sanchunhui.psc.data.protocol.UserInfo
import com.sanchunhui.psc.event.AddSearchMemberEvent
import com.sanchunhui.psc.event.UpdateSelectMemberCountEvent
import com.sanchunhui.psc.utils.GlideUtils
import org.jetbrains.anko.find

/**
 * @Author zyx
 * @Date 2021/7/13-14:23
 * @Dscribe
 */
class SearchMemberAdapter (val context: FragmentActivity?) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var index = 0//当前选中的item数
    private var intentStr : String = "0"

    var mDatas: MutableList<UserInfo> = ArrayList()
    fun setData(data : MutableList<UserInfo>) {
        mDatas = data
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_search_members, parent, false)
        return  SearchMemberViewHolder(view)
    }

    override fun getItemCount(): Int {
        return mDatas.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as SearchMemberViewHolder).bindData(mDatas[position], position)
    }

    inner class SearchMemberViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        val iv_check : ImageView
        val iv_avatar : ImageView
        val tv_userName : TextView
//        val bt_add : Button
        lateinit var userInfo: UserInfo

        init {
            iv_check = itemView.find((R.id.iv_check))
            iv_avatar = itemView.find(R.id.avatar)
            tv_userName = itemView.find(R.id.userName)
//            bt_add = itemView.find(R.id.add)
//            bt_add.setOnClickListener {
//                Bus.send(AddSearchMemberEvent(userInfo.id, userInfo.name.toString()))
//            }

        }

        fun bindData(userInfo: UserInfo, position: Int) {
            this.userInfo = userInfo
            if (userInfo.isSelected) {
                iv_check.setBackgroundResource(R.drawable.icon_box_checked)
            } else {
                iv_check.setBackgroundResource(R.drawable.icon_box_nor)
            }

            itemView.setOnClickListener {

                if (userInfo.isSelected) {
                    index--
                    userInfo.isSelected = false
                    iv_check.setBackgroundResource(R.drawable.icon_box_nor)
                    Bus.send(UpdateSelectMemberCountEvent(index))
                    if (intentStr.equals("1")) {
                        Bus.send(AddSearchMemberEvent(0,""))
                    }
                } else {

                    index++
                    if (intentStr.equals("1") && index > 1) {
                        index--
                        Toast.makeText(context, "组长只能选择一个人", Toast.LENGTH_SHORT).show();
                        return@setOnClickListener
                    }
                    userInfo.isSelected = true
                    iv_check.setBackgroundResource(R.drawable.icon_box_checked)
                    Bus.send(UpdateSelectMemberCountEvent(index))
                    if (intentStr.equals("1")) {
                        Bus.send(AddSearchMemberEvent(userInfo.id, userInfo.name.toString()))
                    }
                }
                notifyItemChanged(position)

            }

            tv_userName.text = userInfo.name
            //TODO:赋值其他字段
            GlideUtils.loadUrlImage(context!!, BaseConstant.IMAGE_SERVER_ADDRESS_URL+"${userInfo.id%1000}/${userInfo.id}.jpg",iv_avatar)
//            Picasso.with(context).load(BaseConstant.IMAGE_SERVER_ADDRESS_URL+"${userInfo.id%1000}/${userInfo.id}.jpg").into(iv_avatar)
        }

    }

    fun setIntentStr(intent: String) {
        intentStr = intent
    }
}